package cn.cdeden.knowledge.controller.recycle;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.controller.recycle.factory.RecycleFactory;
import cn.cdeden.knowledge.controller.recycle.factory.RecycleInterface;
import cn.cdeden.knowledge.domain.FilesTreeRecycle;
import cn.cdeden.knowledge.domain.dto.FilesTreeRecycleParam;
import cn.cdeden.knowledge.domain.vo.FilesTreeRecycleVo;
import cn.cdeden.knowledge.service.FilesTreeRecycleService;
import cn.cdeden.knowledge.service.PersonalFilesTreeService;
import cn.cdeden.resource.api.RemoteRecentlyFileDeleteService;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一级回收站处理模块
 */
@Controller
@Slf4j
//@Api(tags = "文件:一级回收站处理模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/recycleBin")
public class FilesTreeRecycleController {


    private final FilesTreeRecycleService recycleService;

    private final RecycleFactory factory ;

    private final PersonalFilesTreeService filesTreeService;

    @DubboReference(check = false)
    private final RemoteRecentlyFileDeleteService remoteRecentlyFileDeleteService;


    /**
     * 查询一级回收站
     * @param type
     * @param param
     * @param pageable
     * @return
     */
//    @Log("查询一级回收站")
//    @ApiOperation("查询一级回收站")
    @GetMapping(value = "/selectRecycle")
    @ResponseBody
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "type",value = "类型 个人文件:personal 群文件:team  企业文件:  ",dataType = "String" ),
//    })
//    @RequestBody FilesTreeRecycleSelect select
    public R selectRecycle(String type , FilesTreeRecycleParam param, Pageable pageable) throws BadRequestException {
        RecycleInterface recycle = null;
        try{
            recycle = factory.getRecycle(type);
        }catch (Exception e){
            throw new BadRequestException("错误请求类型");
        }
        try{
            TableDataInfo<FilesTreeRecycleVo> info = recycle.selectRecycleAll(param, pageable);
//            PageInfo<FilesTreeRecycleVo> info = recycle.selectRecycleAll(param, pageable);
            return R.ok(info);
//            return BaseRespResult.successResultPage(info.getContent(), info.getTotalElements(), info.getSize());
        }catch (Exception e){
            throw new BadRequestException("查询失败");
        }
    }


    /**
     * 删除
     * @param ids
     * @param type
     * @param info
     * @return
     * @throws BadRequestException
     */
    @ResponseBody
    @GetMapping(value = "/removeFile")
    @Transactional
    public R removeFile(@RequestParam("ids") List<String> ids, @RequestParam("type") String type, @RequestParam(value = "info", required = false) String info) throws BadRequestException {

        if (CollectionUtils.isEmpty(ids)) {
            throw new BadRequestException("删除数据为空");
        }
        RecycleInterface recycle = null ;
        try{
            recycle = factory.getRecycle(type);
        }catch (Exception e){
            throw new BadRequestException("错误请求类型");
        }

        boolean b = recycle.deleteBatch(ids,info);

        //删除文件的查看记录和下载记录
        remoteRecentlyFileDeleteService.deleteByModuleId(ids , OssModuleType.Personal);
        return R.ok();

    }


    /**
     * 一级回收站回收站数据移动到二级回收站
     * @param ids
     * @param recycleType
     * @param type
     * @param teamId
     * @return
     */
//    @Log("一级回收站回收站数据移动到二级回收站")
//    @ApiOperation("一级回收站回收站数据移动到二级回收站")
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "ids",value = "删除ID数组",dataType ="List",required = false),
//        @ApiImplicitParam(name = "recycleType",value = "区别全部删除还是删 选择中删除 0 全部删除1  ",dataType ="String",required = true ,defaultValue = "0"),
//        @ApiImplicitParam(name = "type",value = "类型 个人文件:personal 群文件:team  企业文件:  ",dataType ="String",required = true ,defaultValue = "0"),
//    })
    @GetMapping(value = "/delete/recycle")
    public R deleteRecycle(@RequestParam(value = "ids" ,required = false) List<String> ids,
                                        @RequestParam(value = "recycleType")String recycleType ,
                                        @RequestParam(value =  "type") String type,
                                        @RequestParam(value = "teamId",required = false) String teamId) throws BadRequestException {
        RecycleInterface recycle = null ;
        try{
            recycle = factory.getRecycle(type);
        }catch (Exception e){
            throw new BadRequestException("错误请求类型");
        }
        try{
            List<FilesTreeRecycle> recycleList = recycle.deleteRecycle(ids, recycleType,teamId);
            //添加到二级回收站
            if (CollectionUtils.isNotEmpty(recycleList)){
                recycleService.saveBatch(recycleList);

                if (StringUtils.equals(type,"personal")){
//                    filesTreeService.calculateSpaceSize(String.valueOf(FileUserUtils.getUser().getId()));
                    filesTreeService.calculateSpaceSize(LoginHelper.getUserId().toString());
                }

            }
            return R.ok();
        }catch ( RuntimeException  e){
            throw e; //放回内部抛出错误
        } catch (Exception e){
            throw new BadRequestException("删除数据错误");
        }
    }


    /**
     * 一级回收站恢复文件
     * @param ids
     * @param type
     * @param teamId
     * @return
     */
//    @Log("一级回收站恢复文件")
//    @ApiOperation("一级回收站恢复文件")
    @PostMapping(value = "/recoverFile")
    @ResponseBody
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "ids",value = "编号",dataType = "String"),
//    })
    public R recoverFile(@RequestParam("ids") List<String> ids,@RequestParam("type") String type,@RequestParam(value = "teamId",required = false) String teamId) throws BadRequestException {
        if (CollectionUtils.isEmpty(ids)){
            throw new BadRequestException("缺少数据");
        }
        RecycleInterface recycle = null;
        try{
            recycle = factory.getRecycle(type);
        }catch (Exception e){
            throw new BadRequestException("错误请求类型");
        }
        try{
            return recycle.recoverFileList(ids,teamId);
        }catch (Exception e){
            throw new BadRequestException("还原数据错误");
        }
    }






}
